import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.net.MalformedURLException;
import java.util.*;

/**
 * Created by Bella on 15/04/2014.
 */
public class Search {

    static WebGraph webGraph = new WebGraph();



    public static void crawl(PageNode pageNode) throws MalformedURLException {
        Queue<PageNode> queue = new ArrayDeque<PageNode>();
        webGraph.addWebPage(pageNode.getNodeId(), pageNode);
        queue.add(pageNode);

        while (webGraph.getWebGraph().size() <= 50 && Royal.royalCouples.size() <= 50) {

            PageNode p = queue.poll();
            if (p == null) {
                break;
            }
            Set<String> stringLinks = p.getStringLinks();
            //int pageLength = p.getPageLength();

            //for every word in this page, update the inverted index.
            //if the url is a Wikipedia page, crawl recursively on this page as well

            for (String link : stringLinks) {

                if (webGraph.getWebGraph().size() > 50 || Royal.royalCouples.size() >50)
                    break;

                PageNode linkedPageNode = new PageNode(link);
              //  if (linkedPageNode.getNodeId().contains("/wiki/Elizabeth_II_of_the_United_Kingdom")
                   //     || p.getNodeId().contains("/wiki/Elizabeth_II_of_the_United_Kingdom")) {
                  //  System.out.println("kaki");
              //  }
                if (linkedPageNode.isRoyal()) {
                    webGraph.addWebPage(linkedPageNode.getNodeId(), linkedPageNode);
                    RoyalCouples couple= new RoyalCouples(link, p.getNodeId());
                    Royal.royalCouples.add(couple);
                    p.addLink(linkedPageNode);
                    //add in and out page nodes for hub and authority question
                    //linkedPageNode.addInPageNode(p);
                    //p.addOutPageNode(linkedPageNode);

                    queue.add(linkedPageNode);
                }

            }

        }
    }

}
